<?php
# file will contain core functions for group search pages

# function that will list all groups with given substring in name
function c_group_list($name){
	
	$user_id = $_SESSION['user_id'];
	
	# get list of groups user already are in
	$part_of = array();
	
	$query = "SELECT GROUP_ID FROM participants
						WHERE USER_ID = '$user_id'"; // TODO make sure it works
	$result = c_db_query($query);
	
	if($result){
		while($row = mysql_fetch_array($result)) 
		{
			$part_of[] = $row[0];
		}
	}
	
	# get list of groups user can join and that match given line
	$groups = array();
	
	$query = "SELECT g.ID, g.NAME, u.NAME FROM groups g JOIN user u ON g.MASTER = u.ID
						WHERE g.NAME like '%$name%'"; // TODO make sure it works
	$result = c_db_query($query);
	
	if($result){
		while($row = mysql_fetch_array($result)) 
		{
			if(!in_array($row[0], $part_of)){ // if user are not already in that group
				$groups[] = array('group_id' => $row[0], 'group_name' => $row[1], 'master_name' => $row[2]);
			}
		}
	}
	
	return $groups;
}
?>